﻿using System;
using System.Windows.Data;

namespace BestCode.Creds.UI.Converters
{
    public class ColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                var result = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString((string)value);
                return result;
            }
            catch (Exception)
            {
                var result = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("Black");
                return result;
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string myHex = new System.Windows.Media.ColorConverter().ConvertToString((System.Windows.Media.Color)value);
            return myHex;
        }
        

        public static string GetColorName(System.Windows.Media.Color color)
        {
            Type colors = typeof(System.Windows.Media.Colors);
            foreach (var prop in colors.GetProperties())
            {
                if (((System.Windows.Media.Color)prop.GetValue(null, null)) == color)
                    return prop.Name;
            }
            //by default
            return "Black";
            //throw new Exception("The provided Color is not named.");
        }
    }


    /*
       var name =  GetColorName(colorPicker.SelectedColor);

            string myHex = new System.Windows.Media.ColorConverter().ConvertToString(colorPicker.SelectedColor);

            var result = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("Red");
            var result1 = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#FFFFFF90");


            //colorPicker.SelectedColorText

     * */





}
